package com.school.activity;

import com.school.component.MyDialog;
import com.school.component.MyProgressDialog;
import com.school.net.Login;
import com.school.util.SharePerferenceUtil;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class LoginActivity extends Activity {

	private EditText username;
	private EditText password;
	private MyProgressDialog myProgressDialog;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		username=(EditText) findViewById(R.id.login_username);
		password=(EditText) findViewById(R.id.login_password);
		if(SharePerferenceUtil.getIsLogin(this)){
			startActivity(new Intent(this,MainActivity.class));
			finish();
		}
	}

	public void login(View view){
		myProgressDialog=new MyProgressDialog(LoginActivity.this, "正在登陆，请稍后...");
		new MyAsyncTaskLogin().execute("");
	}

	public class MyAsyncTaskLogin extends AsyncTask<String, String, Boolean>{

		@Override
		protected Boolean doInBackground(String... params) {
			String name=username.getText().toString();
			String pwd=password.getText().toString();
			if(password.getText().toString().length()>12){
				pwd=pwd.substring(0, 12);
			}
			System.out.println(pwd);
			boolean success=new Login().loginStudy(name,pwd);
			return success;
		}

		@Override
		protected void onPostExecute(Boolean success) {
			myProgressDialog.close();
			if(success){
				SharePerferenceUtil.addIsLogin(LoginActivity.this,true);
				String pwd = password.getText().toString();
				if(password.getText().toString().length()>12){
					pwd=pwd.substring(0, 12);
				}
				SharePerferenceUtil.addLoginStudy(LoginActivity.this, username.getText().toString(), pwd);
				startActivity(new Intent(LoginActivity.this,MainActivity.class));
				finish();
			}else{
				MyDialog myDialog=new MyDialog(LoginActivity.this,"提示", "用户名密码错误，请重新输入", "确定", null);
				myDialog.show();
			}
		}

	}
}
